import {
    queryOrderCost, deleteOrderCost, applyOrderCost, confirmCost, addOrderCost, updateOrderCost
} from "../../services/order/orderCost";
import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');
export default {
    namespace: 'orderCost',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: []
    },
    reducers: {
        /**
         * 设置费用列表
         *
         * @param state
         * @param action
         */
        setCostList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        }
    },
    effects: {
        /**
         * 分页查询费用列表
         *
         * @param call
         * @param put
         * @param select
         */
        *queryOrderCost({ payload }, { call, put, select }) {
            let page = yield select(state => state.orderCost.page);
            payload.page = R.isNil(payload.page) ? page : payload.page;
            const res = yield call(queryOrderCost, { ...payload, page: payload.page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let costList = R.isNil(content.list) ? [] : content.list;
                yield put({
                    type: 'setCostList',
                    payload: {
                        list: costList,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 添加费用
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addOrderCost({payload}, { call, put, select }) {
            const res = yield call(addOrderCost, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("添加费用成功！");
                yield put({
                    type: 'queryOrderCost',
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 更新订单费用
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *updateOrderCost({ payload }, { call, put, select }) {
            const res = yield call(updateOrderCost, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("更新费用成功！");
                if(payload.onSuccess()) payload.onSuccess();
                yield put({
                    type: 'queryOrderCost',
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 删除费用
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteOrderCost({ payload }, { call, put, select }) {
            const res = yield call(deleteOrderCost, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");
                yield put({
                    type: "queryOrderCost",
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 申报/审批 费用
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *applyOrderCost({ payload }, { call, put, select }) {
            const res = yield call(applyOrderCost, { data: payload.data });
            if (res.data.resultCode === "ok") {
                message.info("审批成功！");
                yield put({
                    type: "queryOrderCost",
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 确认费用收齐
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *confirmCost({ payload }, { call, put, select }) {
            const res = yield call(confirmCost, { id: payload.id, chargeStatus: payload.status });
            if (res.data.resultCode === "ok") {
                message.info(payload.status === "1" ? "通关费用确认申报完毕!" : "跟单费用确认申报完毕!");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

    }
};